<template>
  <div class="container-fluid">
    <div class="row school-model school-tiao">
      <div class="col school-tiao-left">
        <span>{{ $route.name }}</span>
      </div>
      <div class="col text-right">
        <a-button @click="add()" type="primary">添加节假日</a-button>
      </div>
    </div>
    <div class="row school-model index-model">
      <div class="col school-col-table">
        <el-table :data="getschoolholiday" style="width: 100%; border: 1px solid #eeeeee">
          <el-table-column prop="ID" label="序号" width="60">
            <template slot-scope="scope">{{scope.$index+1}}</template>>
          </el-table-column>
          <el-table-column prop="name" label="节假日名称"></el-table-column>
          <el-table-column prop="name" label="开始日期">
            <template slot-scope="scope">{{ totime(scope.row.starttime) }}</template>
          </el-table-column>
          <el-table-column prop="name" label="结束日期">
            <template slot-scope="scope">{{ totime(scope.row.endtime) }}</template>
          </el-table-column>
          <el-table-column align="right" prop="detailedaddress" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="add(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="del(scope.row.ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog class="school-form" :title="title" width="510px" :visible.sync="setaddress">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" size="mini">
            <el-form-item label="节假日名称:" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="开始日期：" prop="starttime">
              <el-date-picker v-model="ruleForm.starttime" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期：" prop="endtime">
              <el-date-picker v-model="ruleForm.endtime" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
            <el-button @click="setaddress = false" size="mini">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  asynccreatschoolholiday,
  asyncdelschoolholiday
} from "../../../../plugins/api.js";
import { totime } from "../../../utils.js";
export default {
  name: "setfestival",
  data() {
    return {
      setaddress: false,
      title: "添加节假日",
      ruleForm: {
        ID: "",
        starttime: "",
        endtime: "",
        name: ""
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        starttime: [
          { required: true, message: "请选择开始时间", trigger: "change" }
        ],
        endtime: [
          { required: true, message: "请选择结束时间", trigger: "change" }
        ]
      }
    };
  },
  computed: mapGetters(["getschoolholiday"]),
  methods: {
    totime,
    add(v) {
      this.setaddress = true;
      if (v) {
        this.title = "修改节假日";
        this.ruleForm = JSON.parse(JSON.stringify(v));
      } else {
        this.title = "添加节假日";
        this.ruleForm = {
          starttime: "",
          endtime: "",
          name: ""
        };
      }
    },
    del(ID) {
      this.$confirm("此操作将永久删除该节假日, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            await asyncdelschoolholiday({ ID });
            await this.$store.dispatch("actionschoolholiday");
            this.$message.success("删除成功");
          } catch (err) {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async submitForm() {
      this.$refs["ruleForm"].validate(async valid => {
        if (valid) {
          if (this.$store.state.state) return;
          this.$store.commit("setstate", true);
          const data = this.ruleForm.ID ? "修改" : " 添加";
          try {
            await asynccreatschoolholiday(this.ruleForm);
            await this.$store.dispatch("actionschoolholiday");
            this.setaddress = false;
            this.$message.success(`${data}成功`);
          } catch (err) {
            this.$message.error(`${data}失败`);
          }
          this.$store.commit("setstate", false);
        } else {
          console.log("error submit!!");

          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.index-model {
  margin-top: 13px;
}
</style>